package com.example.qixinshangwutong.view.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qixinshangwutong.MainActivity;
import com.example.qixinshangwutong.R;
import com.example.qixinshangwutong.presenter.FourFiguresNumberCode;

import org.jetbrains.annotations.NotNull;

import cn.leancloud.LCUser;
import cn.leancloud.sms.LCSMS;
import cn.leancloud.sms.LCSMSOption;
import cn.leancloud.types.LCNull;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RegisterActivity extends AppCompatActivity {
    private String code;

    public static final String TAG = RegisterActivity.class.getSimpleName();

    private LCSMSOption mOption;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        intiView();
        initData();

    }

    private void initData(){

        mOption = new LCSMSOption();

        mOption.setTtl(10);

        mOption.setApplicationName("qxswtSMS");

        mOption.setOperation("注册验证");

    }
    private void intiView(){
        FourFiguresNumberCode fourFiguresNumberCode = new FourFiguresNumberCode();

        ImageView ivCode = findViewById(R.id.iv_code);

        Button btnGetPhoneCode = findViewById(R.id.register_get_phone_code_btn);

        EditText userNameEdit = findViewById(R.id.register_username_text);

        Button btnRegister = findViewById(R.id.register_btn);

        EditText phoneCodText = findViewById(R.id.register_phone_code_text);

        EditText numberCodeText = findViewById(R.id.register_code_text);

        TextView resToLogin = findViewById(R.id.register_to_login_btn);

        ImageButton goBackBtn = findViewById(R.id.gou_back_btn_register);

        ivCode.setImageBitmap(fourFiguresNumberCode.getInstance().createBitmap());

        code = FourFiguresNumberCode.getInstance().getCode();
        ivCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ivCode.setImageBitmap(fourFiguresNumberCode.getInstance().createBitmap());

                code = FourFiguresNumberCode.getInstance().getCode();

                Log.d(TAG,"Code" + code);

            }
        });
        btnGetPhoneCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String userName = userNameEdit.getText().toString();

                if(userName.isEmpty()){

                    Toast.makeText(RegisterActivity.this,"手机号码不能为空",Toast.LENGTH_LONG).show();
                    return;

                }

                if(userName.length() != 11){

                    Toast.makeText(RegisterActivity.this,"手机号码不能为空",Toast.LENGTH_LONG).show();
                    return;

                }

                LCSMS.requestSMSCodeInBackground(userName,mOption)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<LCNull>() {
                            @Override
                            public void onSubscribe(@NotNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NotNull LCNull lcNull) {

                                Toast.makeText(RegisterActivity.this, "短信发送成功", Toast.LENGTH_SHORT).show();

                            }

                            @Override
                            public void onError(@NotNull Throwable e) {

                                Toast.makeText(RegisterActivity.this, "短信发送失败", Toast.LENGTH_SHORT).show();
                                Log.d(TAG, "error" + e.getMessage(), e);

                            }

                            @Override
                            public void onComplete() {

                            }
                        });

            }
        });
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String phone =  userNameEdit.getText().toString();

                String phoneCode = phoneCodText.getText().toString();

                String numCode = numberCodeText.getText().toString();

                if (numCode.equals(code)){

                    LCUser.signUpOrLoginByMobilePhoneInBackground(phone,phoneCode)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Observer<LCUser>() {
                                @Override
                                public void onSubscribe(@NotNull Disposable d) {

                                }

                                @Override
                                public void onNext(@NotNull LCUser lcUser) {
                                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show();

                                    Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);

                                    startActivity(intent);
                                }

                                @Override
                                public void onError(@NotNull Throwable e) {
                                    Toast.makeText(RegisterActivity.this,"短信验证码不正确",Toast.LENGTH_LONG).show();
                                }

                                @Override
                                public void onComplete() {

                                }
                            });

                    /*LCSMS.verifySMSCodeInBackground(phoneCode,phone)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Observer<LCNull>() {
                                @Override
                                public void onSubscribe(@NotNull Disposable d) {

                                }

                                @Override
                                public void onNext(@NotNull LCNull lcNull) {
                                    Toast.makeText(MainActivity.this,"短信验证码正确",Toast.LENGTH_LONG).show();
                                }

                                @Override
                                public void onError(@NotNull Throwable e) {
                                    Toast.makeText(MainActivity.this,"短信验证码错误",Toast.LENGTH_LONG).show();
                                }

                                @Override
                                public void onComplete() {

                                }
                            });*/


                }

                if (!numCode.equals(code)){

                    Toast.makeText(RegisterActivity.this,"数字验证码不正确",Toast.LENGTH_LONG).show();

                }

                if (numCode.isEmpty()){

                    Toast.makeText(RegisterActivity.this,"验证码不能为空",Toast.LENGTH_LONG).show();

                }

                if (phoneCode.isEmpty()){

                    Toast.makeText(RegisterActivity.this,"手机验证码不能为空",Toast.LENGTH_LONG).show();

                }

            }
        });
        resToLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);

                startActivity(intent);

            }
        });


        goBackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this,BottomActivity.class);

                startActivity(intent);
            }
        });



    }

}